//
//  Layer.swift
//  FramerSwift
//
//  Created by ym on 16/3/20.
//  Copyright © 2016年 ym. All rights reserved.
//

import UIKit


class Layer:UIImageView {
    var name:String! = "layer"
    
    typealias CallBack = () -> ()
    var touchDown:CallBack!
    var touchUpInSide:CallBack!
    var touchDrag:CallBack!
    var heightImage:UIImage?
    var normalImage:UIImage?
    var heightColor:UIColor?
    init(){
        super.init(frame: CGRectMake(5, 5, 20, 20))
    }
    
    init(imageName:String){
        super.init(frame: CGRectMake(0, 0, 100, 100))
        self.image = UIImage(named: imageName)
        self.sizeToFit()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        if let touch = touches.first {
            let location = touch.locationInView(self)
            if CGRectContainsPoint(self.frame, location) {
                if (touchDown != nil) {
                    touchDown()
                }
            }
        }
        if (heightImage != nil) {
            self.normalImage = self.image
            self.image = heightImage
        }
    }
    
    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
        if (touchDrag != nil) {
            touchDrag()
        }
    }
    
    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
        if let touch = touches.first {
            let location = touch.locationInView(self)
            if CGRectContainsPoint(self.bounds, location) {
                if (touchUpInSide != nil) {
                    touchUpInSide()
                }
            }
        }
        if (self.image == heightImage) {
            self.image = normalImage
        }
    }
    
    
}
